/*
 * Created on Feb 21, 2005
 */
package com.scalar.chiptrack.finance.openpo.actioncommands;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.finance.openpo.dao.PurchaseOrderDAO;
import com.scalar.chiptrack.finance.openpo.forms.OpenPurchaseOrderForm;
import com.scalar.chiptrack.users.ErrorReport;
import com.scalar.chiptrack.users.dao.UserDAO;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.commons.ClientInfo;

/**
 * @author Ravikanth
**/
public class OpenPurchaseOrderPrinterFriendlyCommand extends ActionCommand
{
	/* 
	 * To load Open Purchase Order for Priter Friendly Version
	 */
	public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
	{		
		HashMap errors			= ( HashMap ) request.getAttribute( ERROR_MESSAGES );
		HashMap info            = ( HashMap ) request.getAttribute( INFO_MESSAGES );
		String actionForward 	= "printerFriendly";
		String action   		= request.getParameter( REQUEST_ACTION );
        String purchaseOrderId  = request.getParameter ( "purchaseOrderId");
		HttpSession mSession 	= request.getSession();
		OpenPurchaseOrderForm purchaseOrderForm = ( OpenPurchaseOrderForm ) form;

		try
		{
			if( action.equalsIgnoreCase( "printerFriendly" ) )
			{
                try
                {
                    purchaseOrderForm.setPurchaseOrder_id( Integer.parseInt( purchaseOrderId));
                }
                catch( Exception e)
                {
                    purchaseOrderForm.setPurchaseOrder_id(0);
                }

				PurchaseOrderDAO.loadOpenPurchaseOrder( purchaseOrderForm );

                ClientInfo clientInfo = UserDAO.getClientAddressInfo();
                purchaseOrderForm.setClientInfo( clientInfo);

                purchaseOrderForm.setShippingAddress( PurchaseOrderDAO.getAddress( purchaseOrderForm.getShippingAddress_id() ) );
                purchaseOrderForm.setBillingAddress( PurchaseOrderDAO.getClientAddress() );
				purchaseOrderForm.setBillingAddress_id( PurchaseOrderDAO.getClientAddress().getAddressId() );

				request.setAttribute ( "financeReasonCodes", PurchaseOrderDAO.loadFinanceHolds() );
				request.setAttribute ( "opsReasonCodes", PurchaseOrderDAO.loadOperationsHolds() );
				actionForward = "printerFriendly";
			}
		}
		catch ( Exception e )
		{
			Logger.error( ERROR_LOADING_OPEN_PURCHASE_ORDER_INFORMATION, e );
			errors.put( ERROR_INTERNAL_SERVER_ERROR, ERROR_LOADING_OPEN_PURCHASE_ORDER_INFORMATION );
			mSession.setAttribute( ERROR_REPORT_OBJECT, new ErrorReport( "Error while executing the command", e ) );
			actionForward = "saveSuccess";
		}
		        
		request.setAttribute( ERROR_MESSAGES, errors );
		request.setAttribute( INFO_MESSAGES, info );
		return mapping.findForward( actionForward );
	}
}
